from database.db_handler import MysqlHander
from common.my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
from common.market_data import MarketData
import glob
import jieba
import re
import threading
import hashlib
import json
from database.db_business import DbBusiness

'''
宏观数据：发展改革委员会
'''

class DongfangcaifuPilu:
    def __init__(self):
        self.market = MarketData()
        self.business = DbBusiness()
        self.code_ex = {}
        self.code_name = {}
    
    def get_url(self, page, date):
        return "https://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery1123028753372694270807_1646558082242&sortColumns=FIRST_APPOINT_DATE,SECURITY_CODE&sortTypes=1,1&pageSize=50&pageNumber=" + str(page) + "&reportName=RPT_PUBLIC_BS_APPOIN&columns=ALL&filter=(SECURITY_TYPE_CODE+in+(\"058001001\",\"058001008\"))(TRADE_MARKET_CODE!=\"069001017\")(REPORT_DATE='" + date + "')"
    
    def get_online_data(self, url):
        proxy = self.business.query_proxy()
        json_data = json.loads(MyHttp.http_json_data_v2(url, proxy).decode().replace("jQuery1123028753372694270807_1646558082242(","").replace(");",""))

        for d in json_data["result"]["data"]:
            if 1 == d["IS_PUBLISH"]:
                return;
            data_title = d["SECURITY_NAME_ABBR"] + "(" +  d["SECURITY_CODE"] + ")披露" + d["REPORT_TYPE_NAME"].replace(" ","")
            data_date = d["FIRST_APPOINT_DATE"][0:10]
            date_del = []
            if d["THIRD_CHANGE_DATE"] != None:
                date_plan = d["THIRD_CHANGE_DATE"][0:10]
                date_del.append(d["SECOND_CHANGE_DATE"][0:10])
                date_del.append(d["FIRST_CHANGE_DATE"][0:10])
                date_del.append(d["FIRST_APPOINT_DATE"][0:10])
            elif d["SECOND_CHANGE_DATE"] != None:
                date_plan = d["SECOND_CHANGE_DATE"][0:10]
                date_del.append(d["FIRST_CHANGE_DATE"][0:10])
                date_del.append(d["FIRST_APPOINT_DATE"][0:10])
            elif d["FIRST_CHANGE_DATE"] != None:
                date_plan = d["FIRST_CHANGE_DATE"][0:10]
                date_del.append(d["FIRST_APPOINT_DATE"][0:10])
            data_url = ""
            self.market.add_market_data(data_date, 2301, data_title, data_url, "东方财富网")
            for del_date in date_del:
                self.market.del_market_data(del_date, 2301, data_title)
        return True

    
    def get_data(self):
        for i in range (1,100):
            try:
                url = self.get_url(i, "2021-12-31")
                self.get_online_data(url)
            except Exception as e:
                print(str(e))
                i = i - 1
        for i in range (1,100):
            try:
                url = self.get_url(i, "2022-03-31")
                self.get_online_data(url)
            except Exception as e:
                print(str(e))
                i = i - 1


if __name__ == '__main__':
    d = DongfangcaifuPilu()
    d.get_data()
